#!/bin/bash

# 测试修复后的API脚本
# 使用方法: bash test-fixed-apis.sh

echo "======================================"
echo "  测试修复后的API"
echo "======================================"
echo ""

# 管理员Token (请根据实际情况更新)
ADMIN_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbklkIjoxLCJ1c2VybmFtZSI6ImFkbWluIiwicm9sZSI6IlNVUEVSX0FETUlOIiwiaWF0IjoxNzYwMTg3OTc1LCJleHAiOjE3NjAyMTY3NzV9.Q7HIaeKJCBe0DkIPD11ECh2G3YaoOVOjH58LE0QGXPk"

# 用户Token (请根据实际情况更新)
USER_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImlkTnVtYmVyIjoiOTkwMTAxLTEyMzQ1NjciLCJpYXQiOjE3NjAxODgwMTMsImV4cCI6MTc2MDI3NDQxM30.pHWe2oEQHM4Qy115OEepknBN2531bxLvWZDas_f65gs"

echo "1️⃣  测试管理端质押列表 (修复问题1)"
echo "-----------------------------------"
curl -s http://localhost:3000/api/admin/staking/staking-list \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.'
echo ""

echo "2️⃣  测试质押产品列表 (修复问题2)"
echo "-----------------------------------"
curl -s http://localhost:3000/api/admin/staking/products \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.'
echo ""

echo "3️⃣  测试用户质押概览 - 验证日收益率 (修复问题3)"
echo "-----------------------------------"
curl -s http://localhost:3000/api/staking/overview \
  -H "Authorization: Bearer $USER_TOKEN" | jq '.'
echo ""

echo "4️⃣  验证数据库中的日收益率"
echo "-----------------------------------"
npx ts-node -e "
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function main() {
  const userStaking = await prisma.userStaking.findFirst({
    where: { userId: 1 }
  });

  if (userStaking) {
    const dailyRate = Number(userStaking.dailyRewardRate);
    const annualRate = dailyRate * 365 * 100;

    console.log('质押本金:', Number(userStaking.stakedAmount), 'NEAR');
    console.log('日收益率:', (dailyRate * 100).toFixed(4) + '%');
    console.log('年化收益率:', annualRate.toFixed(2) + '%');
    console.log('');
    console.log('✅ 日收益率正确:', dailyRate.toFixed(10));
  }

  await prisma.\$disconnect();
}

main();
"

echo ""
echo "======================================"
echo "  测试完成！"
echo "======================================"
